package com.emmmya.ocs.modules.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.emmmya.harinCheck.annotation.Check;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.emmmya.ocs.common.base.BaseEntity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Data
@Entity
@TableName("h_user")
@Table(name = "h_user")
@JsonIgnoreProperties(value={"roles"})
public class User extends BaseEntity {
    @Column(name = "username", unique = true, nullable = false)
    @Check(name = "用户名-账号")
    @TableField("username")
    private String username;

    @Column(name = "password", nullable = false, updatable = false)
    @Check(name = "密码")
    @TableField("password")
    private String password;

    @Column(name="profile_url")
    @Check(name = "头像文件url地址")
    @TableField("profile_url")
    private String profileUrl;

    @Column(name = "status")
    @Check(name = "状态 1 正常  0  禁用")
    @TableField("status")
    private Integer status = 1;

    @Transient
    @TableField(exist = false)
    @Check(name = "登录记住我标识")
    private String rememberMe;
    
    @Column(name = "name", nullable = false)
    @Check(name = "用户姓名")
    @TableField("name")
    private String name;

    @Column(name="password_invalid_interval")
    @Check(name = "密码过期间隔")
    @TableField("password_invalid_interval")
    private Integer passwordInvalidInterval;


    @Column(name = "sex")
    @Check(name = "性别")
    @TableField("sex")
    private String sex;


    @Column(name = "email")
    @Check(name = "邮箱")
    @TableField("email")
    //@Email
    private String email;

    @Column(name="department_id")
    @Check(name = "所属部门id")
    @TableField("department_id")
    private String departmentId;

    @Transient
    @TableField(exist=false)
    @Check(name = "所属部门名称")
    private String departmentTitle;

    @Column(name="tel_phone")
    @Check(name = "联系电话")
    @TableField("tel_phone")
    private Date telPhone;

    @Column(name="last_login_time")
    @Check(name = "最后登录时间")
    @TableField("last_login_time")
    private Date lastLoginTime;

    @Column(name="last_update_pwd_time")
    @Check(name = "最后更新密码日期")
    @TableField("last_update_pwd_time")
    private Date lastUpdatePwdTime;

    @Column(name="login_max_attempt")
    @Check(name = "最大登录次数")
    @TableField("login_max_attempt")
    private Integer loginMaxAttempt;

    @Column(name="work_card_id")
    @Check(name = "工牌id")
    @TableField("work_card_id")
    private String workCardId;

    @Transient
    @TableField(exist = false)
    @Check(name = "陆前的token")
    private String loginToken;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "h_user_role",//关联表
            inverseJoinColumns = {@JoinColumn(name = "role_id")}, //关联表名
            joinColumns = {@JoinColumn(name = "user_id")})//维护端外键
    @JsonBackReference
    @JsonIgnore //json解析过滤此项
    @TableField(exist = false)
    @Check(name = "用户对应的角色集合")
    private List<Role> roles;

    @TableField(exist = false)
    @Transient
    @Check(name="app存储角色")
    private Set<String> appRoles;

    @TableField(exist = false)
    @Transient
    @Check(name="app存储权限")
    private Set<String> appPermissions;



    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", profileUrl='" + profileUrl + '\'' +
                ", status=" + status +
                ", rememberMe='" + rememberMe + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", email='" + email + '\'' +
                ", departmentId='" + departmentId + '\'' +
                ", departmentTitle='" + departmentTitle + '\'' +
                ", lastLoginTime=" + lastLoginTime +
                ", lastUpdatePwdTime=" + lastUpdatePwdTime +
                ", loginMaxAttempt=" + loginMaxAttempt +
                ", passwordInvalidInterval=" + passwordInvalidInterval +
                ", loginToken='" + loginToken + '\'' +
                '}';
    }
}
